from functools import partial

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QDialog, QShortcut


class MessageDialog(QDialog):
    def __init__(self, parent=None):
        super(MessageDialog, self).__init__(parent)
        self.content_label = None
        self.grid = None
        self.init_ui()

    def closeEvent(self, event):
        self.done(1024)

    def init_ui(self):
        self.grid = QtWidgets.QGridLayout(self)
        QShortcut('Esc', self).activated.connect(self.close)

    def setText(self, text: str):
        self.content_label = QtWidgets.QLabel(text, self)
        self.grid.addWidget(self.content_label, 0, 0)

    def add_buttons(self, items: list):
        splitter = QtWidgets.QSplitter(self)
        # splitter.setChildrenCollapsible(False)
        splitter.setHandleWidth(3)
        for i, item in enumerate(items):
            btn = QtWidgets.QPushButton(item, self)
            btn.clicked.connect(partial(self.select_content, i))
            splitter.addWidget(btn)
        self.grid.addWidget(splitter, 1, 0)

    def select_content(self, i):
        self.done(i)


if __name__ == '__main__':
    import sys
    from PyQt5.QtWidgets import QApplication
    app = QApplication(sys.argv)
    msg_dialog = MessageDialog(None)
    # msg_dialog.setWindowIcon(WINDOW_TITLE_ICON)
    # msg_dialog.setWindowTitle(WINDOW_TITLE)
    msg_dialog.setText("To see more...")
    msg_dialog.add_buttons(["", "", "OK", "Cancel"])
    msg_dialog.show()
    app.exec()
